package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    public R<Page<SetmealDto>> findAll(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmeaDtolList = setmealService.findAll(page, pageSize, name);
        return R.success(setmeaDtolList);
    }

    //批量删除
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(Long[] ids, HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        setmealService.delete(ids, id);
        return R.success("删除套餐成功");
    }

    //根据id修改售卖状态
    @RequestMapping("/status/{status}")
    public R<String> updateStatus(Long[] ids,@PathVariable Integer status) {
        setmealService.updateStatus(ids,status);
        return R.success("修改状态成功");
    }

//    //根据id修改售卖状态
//    @RequestMapping("/status/1")
//    public R<String> updateStatus2(Long[] ids) {
//        setmealService.updateStatus2(ids);
//        return R.success("修改状态成功");
//    }

    //数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    //修改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(id);
        setmealService.updateSetmealAndSetmealDish(setmealDto);
        return R.success("修改成功");
    }

    //根据分类id查询套餐
    @GetMapping("/list")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> findByCategoryIdAndStatus(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealService.findByCategoryIdAndStatus(categoryId, status);
        return R.success(setmealList);
    }
}
